package com.example.anew.journey.mynewjourney.logic.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.adpater.EquipmentAdapter;
import com.example.anew.journey.mynewjourney.customview.MyRecyclerView;
import com.example.anew.journey.mynewjourney.logic.addDevices.AddDeviecsActivity;
import com.example.anew.journey.mynewjourney.utils.AntiShake;
import com.jcodecraeer.xrecyclerview.ProgressStyle;

import butterknife.ButterKnife;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/7
 */

public class EquipmentFragment extends Fragment {
    public Context mcontext;
    AntiShake util;
    private ImageView iv_add_devices;
    private TextView tv_add_devices;
    private int p = 1;
    private MyRecyclerView fragmentEquipmentMyRecyclerview;
    private EquipmentAdapter equipmentAdapter;
    private RelativeLayout rl_no_date;

    /**
     * @desc 当Fragment被添加到Activity的时候调用
     * @desc 调用一次
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mcontext = context;
        // 防抖动工具类
        util = new AntiShake();
    }

    /**
     * @desc 创建Fragment的时候调用和绘制view的时候调用
     * @desc 有可能调用多次
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mcontext,R.layout.fragment_equipment,null); //fragment_equipment
       initRecyclerView(view,container);
        findEmptyView(view);
        return view;
    }

    private void initRecyclerView(View view, ViewGroup container) {
        fragmentEquipmentMyRecyclerview = view.findViewById(R.id.fragment_equipment_my_recyclerview);
       // rl_no_date = view.findViewById(R.id.fragment_empty_view_rl_no_date);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mcontext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentEquipmentMyRecyclerview.setLayoutManager(layoutManager);
        fragmentEquipmentMyRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        fragmentEquipmentMyRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        // fragmentEquipmentMyRecyclerview.setArrowImageView(R.mipmap.iconfont_downgrey);
        fragmentEquipmentMyRecyclerview.setPullRefreshEnabled(false);  //TODO 禁止下拉刷新完毕
        fragmentEquipmentMyRecyclerview.getDefaultFootView().setNoMoreHint("---我是有底线的---");
        View empty_view = LayoutInflater.from(mcontext).inflate(R.layout.fragment_empty_view, container,false);
        findEmptyView(view);
        equipmentAdapter = new EquipmentAdapter();
        fragmentEquipmentMyRecyclerview.setAdapter(equipmentAdapter);
        fragmentEquipmentMyRecyclerview.setEmptyView(empty_view);
        equipmentAdapter.notifyDataSetChanged();
    }
    /**
     * @desc 空数据的时候
     * */
    private void findEmptyView(View emptyView) {
        iv_add_devices = (ImageView) emptyView.findViewById(R.id.fragment_empty_view_iv_add_devices);
        tv_add_devices = (TextView)emptyView.findViewById(R.id.fragment_empty_view_tv_add_devices);
        iv_add_devices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toAddDevicesActivity();
            }
        });
    }

    /**
     * @desc 创建Fragment和绘制view好后的时候调用
     * @desc 有可能调用多次
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initDate();
    }
    /**
     * @数据初始化
     * */
    private void initDate() {
        loadDate(p);
    }
    /**
     * @desc 加载数据
     * */
    private void loadDate(int p) {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    // 跳转到添加设备页面
    private void toAddDevicesActivity() {
           Intent intent =  new Intent(mcontext,AddDeviecsActivity.class);
          startActivity(intent);
    }

}
